<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      思路：定义一个函数，计算每个位的平方和。定义一个map存放每一次的值，如果有重复的，那么就说明陷入无限循环了
      */
      var isHappy = function (n) {
        let map = new Map()
        function fn(n) {
          let result = 0
          while (n) {
            result += (n % 10) ** 2
            n = parseInt(n / 10)
          }
          return result
        }
        //这是第一次的结果
        let res = fn(n)
        //后面进行递归
        while (true) {
          if (map.has(res)) {
            return false
          }
          if (res == 1) {
            return true
          }
          map.set(res, 1)
          res = fn(res)
        }
      }
    </script>
  </body>
</html>
